package com.qidi.jtademo;

import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/8/11.
 *
 * 数据源的配置
 *
 */
@Configuration
public class DataSourceConfig {
 

    private  DataSourceProperties dataSourceProperties;



    /**
     * 第一种方法
     * @return
     */
    @Primary
    @Bean(name = "primaryDataSourceProperties")
    @Qualifier("primaryDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSourceProperties primaryDataSourceProperties() {
        DataSourceProperties properties =  new DataSourceProperties();
        this.dataSourceProperties= properties;
        return properties;
    }

    @Primary
    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {
        DataSource dataSource =  primaryDataSourceProperties().initializeDataSourceBuilder().build();

        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();

        mysqlXaDataSource.setUrl(dataSourceProperties.getUrl());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXaDataSource.setPassword(dataSourceProperties.getPassword());
        mysqlXaDataSource.setUser(dataSourceProperties.getUsername());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);


        AtomikosDataSourceBean atomikosDataSourceBean=new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(mysqlXaDataSource);
        atomikosDataSourceBean.setUniqueResourceName("primaryDataSource");

 /*       atomikosDataSourceBean.setMinPoolSize(dataSourceProperties.getMinPoolSize());
        atomikosDataSourceBean.setMaxPoolSize(config1.getMaxPoolSize());
        atomikosDataSourceBean.setMaxLifetime(config1.getMaxLifetime());
        atomikosDataSourceBean.setBorrowConnectionTimeout(config1.getBorrowConnectionTimeout());
        atomikosDataSourceBean.setLoginTimeout(config1.getLoginTimeout());
        atomikosDataSourceBean.setMaintenanceInterval(config1.getMaintenanceInterval());
        atomikosDataSourceBean.setMaxIdleTime(config1.getMaxIdleTime());*/
        return  atomikosDataSourceBean;
    }






    /**
     * 第二种方法
     * @return
     */
    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource secondaryDataSource(DBConfig2 config2)  throws SQLException {
        //return DataSourceBuilder.create().type(HikariDataSource.class).build();



        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();

        mysqlXaDataSource.setUrl(config2.getUrl());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXaDataSource.setPassword(config2.getPassword());
        mysqlXaDataSource.setUser(config2.getUsername());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);


        AtomikosDataSourceBean atomikosDataSourceBean=new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(mysqlXaDataSource);
        atomikosDataSourceBean.setUniqueResourceName("secondaryDataSource");

        atomikosDataSourceBean.setMinPoolSize(config2.getMinPoolSize());
        atomikosDataSourceBean.setMaxPoolSize(config2.getMaxPoolSize());
        atomikosDataSourceBean.setMaxLifetime(config2.getMaxLifetime());
        atomikosDataSourceBean.setBorrowConnectionTimeout(config2.getBorrowConnectionTimeout());
        atomikosDataSourceBean.setLoginTimeout(config2.getLoginTimeout());
        atomikosDataSourceBean.setMaintenanceInterval(config2.getMaintenanceInterval());
        atomikosDataSourceBean.setMaxIdleTime(config2.getMaxIdleTime());
        atomikosDataSourceBean.setTestQuery(config2.getTestQuery());

        return DataSourceBuilder.create().build();
    }


}